import 'package:flutter/material.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

///Title:常规页面状态布局 <br/>
///Description: <br/>
///author:xiemiao <br/>
///date: 2020-02-26 <br/>
///version 1.0 <br/>
class PageStateWidget extends StatefulWidget {
  /// 控制器
  final PageStateController controller;

  final Widget child;

  ///重试点击
  final VoidCallback retryClick;

  ///是否展示重试按钮
  final bool showRetry;

  ///重试widget
  final Widget retryWidget;

  ///图片
  final String image;

  ///提示
  final String tip;

  ///页面状态
  final PageStateType state;

  PageStateWidget({
    Key key,
    @required this.child,
    this.controller,
    this.retryClick,
    this.showRetry = true,
    this.image,
    this.tip,
    this.state = PageStateType.LOADING,
    this.retryWidget,
  }) : super(key: key);

  @override
  _PageStateWidgetState createState() => _PageStateWidgetState();
}

class _PageStateWidgetState extends State<PageStateWidget> {
  String _image;
  String _tip;
  bool _showRetry = true;
  PageStateType _state;

  @override
  void initState() {
    super.initState();
    _state = widget.state;
    _image = widget.image;
    _tip = widget.tip;
    _showRetry = widget.showRetry;
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 绑定控制器
    widget.controller?._bindPageStateWidgetState(this);
  }

  @override
  Widget build(BuildContext context) {
    Widget resultWidget = Gaps.empty;
    switch (_state) {
      case PageStateType.LOADING:
        resultWidget = _buildLoading();
        break;
      case PageStateType.EMPTY:
        _image ??= R.assetsImgCommonImgEmpty;
        _tip ??= "暂无数据,请重试!!!";
        resultWidget = _buildImageTipRetry();
        break;
      case PageStateType.ERROR:
        _image ??= R.assetsImgCommonImgError;
        _tip ??= "网络异常,请重试!!!";
        resultWidget = _buildImageTipRetry();
        break;
      case PageStateType.SUCCESS:
        resultWidget = widget.child;
        break;
    }
    return resultWidget;
  }

  ///构建图片提示重试widget
  Widget _buildImageTipRetry() {
    Widget _retryWidget = Gaps.empty;

    if (_showRetry) {
      _retryWidget = widget.retryWidget ??
          RaisedButton(
            onPressed: widget.retryClick,
            child: Text("重试"),
          );
    }

    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 10),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            _image,
            width: 100,
          ),
          Gaps.v_gap_10,
          Container(
            width: 200,
            child: Text(
              _tip,
              style: TextStyle(
                fontSize: CommonDimens.font_size_small,
                color: CommonColors.black,
              ),
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
              textAlign: TextAlign.center,
            ),
          ),
          _retryWidget,
        ],
      ),
    );
  }

  ///构建loading
  Widget _buildLoading() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SpinKitCircle(
            color: CommonColors.primary,
            size: 60,
          ),
          Gaps.v_gap_10,
          Text("加载中...",
              style: TextStyle(
                fontSize: CommonDimens.font_size_small,
                color: CommonColors.black,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis)
        ],
      ),
    );
  }

  ///展示加载中状态
  void showLoadingState() {
    setState(() {
      _state = PageStateType.LOADING;
    });
  }

  ///展示空状态
  void showEmptyState({String image, String tip, bool showRetry}) {
    setState(() {
      _image = image;
      _tip = tip;
      _showRetry = showRetry ?? true;
      _state = PageStateType.EMPTY;
    });
  }

  ///展示异常状态
  void showErrorState({String image, String tip, bool showRetry}) {
    setState(() {
      _image = image;
      _tip = tip;
      _showRetry = showRetry ?? true;
      _state = PageStateType.ERROR;
    });
  }

  ///展示成功状态
  void showSuccessState() {
    setState(() {
      _state = PageStateType.SUCCESS;
    });
  }

  ///改变页面状态类型
  void changePageStateType(PageStateType pageStateType) {
    setState(() {
      _state = pageStateType;
    });
  }
}

///页面状态控制器
class PageStateController {
  _PageStateWidgetState _pageStateWidgetState;

  ///绑定页面状态
  void _bindPageStateWidgetState(_PageStateWidgetState _pageStateWidgetState) {
    this._pageStateWidgetState = _pageStateWidgetState;
  }

  ///展示加载中
  void showLoading() {
    _pageStateWidgetState?.showLoadingState();
  }

  ///展示empty
  void showEmpty({String image, String tip, bool showRetry}) {
    _pageStateWidgetState?.showEmptyState(image: image, tip: tip, showRetry: showRetry);
  }

  ///展示error
  void showError(String tip, {String image, bool showRetry}) {
    _pageStateWidgetState?.showErrorState(image: image, tip: tip, showRetry: showRetry);
  }

  ///展示success
  void showSuccess() {
    _pageStateWidgetState?.showSuccessState();
  }

  ///改变页面状态类型
  void changePageStateType(PageStateType pageStateType) {
    _pageStateWidgetState?.changePageStateType(pageStateType);
  }
}

///页面状态类型
enum PageStateType {
  ///加载中
  LOADING,

  ///空布局
  EMPTY,

  ///异常布局
  ERROR,

  ///成功布局
  SUCCESS,
}
